package com.windward.vii.web.controller;

import com.windward.vii.common.Constants;
import com.windward.vii.common.JsonResult;
import com.windward.vii.common.ResultCode;
import com.windward.vii.service.GiftService;
import com.windward.vii.service.UserTokenService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * Created by tiansj on 2017/8/26.
 */
@Controller
public class GiftController {

    @Resource
    GiftService giftService;
    @Resource
    UserTokenService userTokenService;

    @RequestMapping(value = "v1/api1/gift/list", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult getGiftList() {
        return giftService.getGiftList();
    }

    /**
     * 用户送礼给主播
     * @param anchorUid
     * @param token
     * @return
     */
    @RequestMapping(value = "v1/api1/gift/send", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult sendGift(@RequestParam Long anchorUid,
                               @RequestParam Long giftId,
                               @RequestParam Integer sourceType,
                               Long chatId,
                               @RequestHeader(value="token") String token) {
        if(anchorUid <= 0) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "anchorUid错误");
        }
        if(giftId <= 0) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "giftId错误");
        }
        if(sourceType < 1 && sourceType > 3) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "来源类型参数错误(1:聊天，2:IM，3:个人主页打赏)");
        }
        if(sourceType == Constants.Gift.SOURCE_TYPE_CHAT) {
            if (chatId == null || chatId <= 0) {
                return new JsonResult(ResultCode.PARAM_ERROR_CODE, "chatId错误");
            }
        }
        Long uid = userTokenService.getUidByToken(token);
        return giftService.sendGift(uid, anchorUid, giftId, sourceType, chatId);
    }
}
